package com.dfbz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.dfbz.entity.User;
import com.dfbz.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@SpringBootTest(classes = MyBatisPlusApplication.class)
@RunWith(SpringRunner.class)
public class Demo06_LambdaQueryMapper {

    @Autowired
    private UserMapper userMapper;

    /**
     * 使用QueryWrapper
     *
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {

        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.eq("id", "1");

        List<User> userList = userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    /**
     * 使用LambdaQueryWrapper
     *
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {

        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();

        // id=1
//        wrapper.eq(User::getId,1);

        // select id,name,age from user where id in (1,2,3) and name like "%a%"
        wrapper.in(User::getId, "1", "2", "3")
                .like(User::getName, "a")
                .select(User::getId, User::getName, User::getAge);

        List<User> userList = userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }
}
